package Utils.word;

import org.apache.poi.xwpf.converter.core.FileImageExtractor;
import org.apache.poi.xwpf.converter.core.FileURIResolver;
import org.apache.poi.xwpf.converter.xhtml.XHTMLConverter;
import org.apache.poi.xwpf.converter.xhtml.XHTMLOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

import java.io.*;

/**
 * Created by 隼 on 2017/6/4.
 * 应用POI，word2007转html
 * http://happyqing.iteye.com/blog/2086450
 *poi3.8
 * maven
 * <dependencies>
 <dependency>
 <groupId> fr.opensagres.xdocreport</groupId>
 <artifactId> org.apache.poi.xwpf.converter.core</artifactId>
 <version> 1.0.4</version>
 </dependency>
 <dependency>
 <groupId> fr.opensagres.xdocreport</groupId>
 <artifactId> org.apache.poi.xwpf.converter.xhtml</artifactId>
 <version> 1.0.4</version>
 </dependency>
 </dependencies>
 */
public class word07toHtml {
    //@Test
    public static void canExtractImage() throws IOException {
        File f = new File("C:\\Users\\隼\\Desktop\\test.docx");
        if (!f.exists()) {
            System.out.println("Sorry File does not Exists!");
        } else {
            if (f.getName().endsWith(".docx") || f.getName().endsWith(".DOCX")) {

                // 1) Load DOCX into XWPFDocument
                InputStream in = new FileInputStream(f);
                XWPFDocument document = new XWPFDocument(in);

                // 2) Prepare XHTML options (here we set the IURIResolver to
                // load images from a "word/media" folder)
                File imageFolderFile = new File("C:\\Users\\隼\\Desktop\\test\\media");
                XHTMLOptions options = XHTMLOptions.create().URIResolver(
                        new FileURIResolver(imageFolderFile));
                options.setExtractor(new FileImageExtractor(imageFolderFile));
                //options.setIgnoreStylesIfUnused(false);
                //options.setFragment(true);

                // 3) Convert XWPFDocument to XHTML
                OutputStream out = new FileOutputStream(new File(
                        "C:\\Users\\隼\\Desktop\\test.htm"));
                XHTMLConverter.getInstance().convert(document, out, options);
            } else {
                System.out.println("Enter only MS Office 2007+ files");
            }
        }
    }

    public static void main(String args[]) {
        try {
            canExtractImage();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}





